package com.hy.z.utils;


import com.hy.z.entity.Resources;
import org.springframework.beans.BeanUtils;

import java.util.ArrayList;
import java.util.List;

public class TreeUtil {

    List<TreeNode> nodes;

    public TreeUtil(List<TreeNode> nodes) {
        super();
        this.nodes = nodes;
    }

    /**
     * 构建树形结构
     *
     * @return
     */
    public List<TreeNode> buildTree() {
        List<TreeNode> treeNodes = new ArrayList<>();
        List<TreeNode> rootNodes = getRootNodes();
        for (TreeNode rootNode : rootNodes) {
            buildChildNodes(rootNode);
            treeNodes.add(rootNode);
        }
        return treeNodes;
    }

    /**
     * 递归子节点
     *
     * @param node
     */
    public void buildChildNodes(TreeNode node) {
        List<TreeNode> children = getChildNodes(node);
        //只递归到二级菜单
        if (!children.isEmpty()) {
            for (TreeNode child : children) {
                buildChildNodes(child);
            }
            node.setChildren(children);

        }
    }

    /**
     * 获取父节点下所有的子节点
     *
     * @param node
     * @return
     */
    public List<TreeNode> getChildNodes(TreeNode node) {
        List<TreeNode> childNodes = new ArrayList<TreeNode>();
        for (TreeNode n : nodes) {
            if (node.getId().equals(n.getParentId())) {
                childNodes.add(n);
            }
        }
        return childNodes;
    }

    /**
     * 判断是否为根节点
     *
     * @return
     */
    public boolean rootNode(TreeNode node) {
        boolean isRootNode = true;
        for (TreeNode n : nodes) {
            if (node.getParentId().equals(n.getId())) {
                isRootNode = false;
                break;
            }
        }
        return isRootNode;
    }

    /**
     * 获取集合中所有的根节点
     *
     * @return
     */
    public List<TreeNode> getRootNodes() {
        List<TreeNode> rootNodes = new ArrayList<TreeNode>();
        for (TreeNode n : nodes) {
            if (rootNode(n)) {
                rootNodes.add(n);
            }
        }
        return rootNodes;
    }
    //构建树形节点
    public static List<TreeNode> getTreeNodes(List<Resources> resources) {
        List<TreeNode> treeNodes = BeanCopyUtil.copyListProperties(resources, TreeNode::new);
        return new TreeUtil(treeNodes).buildTree();
    }


}
